package net.zjitc.action;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import net.zjitc.domain.User;
import net.zjitc.service.UserService;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by lkj10 on 2017/2/28.
 */
@Controller
public class UserAction extends ActionSupport implements ModelDriven<User> {
    private String message;


    private User model=new User();

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }


    @Resource
    public UserService userService;

    public String loginUI() {

        return "loginUI";
    }

    public String login() {
        boolean b = userService.fingByUser(model.getUsername());
        /*放入值栈空间的对象栈中*/
        ActionContext.getContext().getValueStack().push(b);
        if (b) {
            message = "帐号已存在，注册失败！";
            return "loginFail";
        } else {

                userService.register(model.getUsername(),model.getPassword());
                message = "注册成功！";
                /*查询所有用户*/
            List<User> userList=userService.findAll();
            /*将查找出来的对象放到值栈空间中*/
            ActionContext.getContext().put("userList",userList);
                return "loginSuccess";

        }
    }

    @Override
    public User getModel() {
        return model;
    }
}



